package com.music.controller;

import com.github.pagehelper.PageInfo;
import com.music.pojo.Singer;
import com.music.pojo.SingleSong;
import com.music.pojo.UserInfo;
import com.music.service.SingerService;
import com.music.service.SingleSongService;
import com.music.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@Controller
@RequestMapping("singer")
public class SingerController {
    @Autowired
    private SingerService singerService;

    @RequestMapping("search")
    @ResponseBody
    public PageInfo<Singer> songPageInfo(String search, Integer pageNum) throws UnsupportedEncodingException {
        String _search = URLDecoder.decode(search,"UTF-8");
        return singerService.findSingerBySearch(_search,pageNum);
    }

    //是否关注歌手
    @RequestMapping("isFollow")
    @ResponseBody
    public ResultInfo isFollow(Integer sid, HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        ResultInfo resultInfo;
        if (userInfo==null){
            return new ResultInfo(false);
        }
        Integer num = singerService.isFollow(sid,userInfo.getUid());
        if (num==0){
            resultInfo = new ResultInfo(false);
        }else {
            resultInfo = new ResultInfo(true);
        }
        resultInfo.setData(num);
        return resultInfo;
    }

    @RequestMapping("singerDetail")
    @ResponseBody
    public ResultInfo singerRetail(Integer sid,HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        ResultInfo resultInfo = new ResultInfo(true);
        Singer singer;
        if (userInfo==null){
            singer = singerService.findSingerWithAlbum(sid,null);
        }else {
            singer = singerService.findSingerWithAlbum(sid,userInfo.getUid());
        }
        resultInfo.setData(singer);
        return resultInfo;
    }

}
